package com.yonyou.pmclouds.offline.configuration;

import com.yonyou.pmclouds.alterandvisa.controller.AlterAndVisaController;
import com.yonyou.pmclouds.offline.aspect.Offline;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationUtils;

import java.lang.annotation.Annotation;

@Configuration
public class OffLineDataDownloadConfig implements BeanPostProcessor{

    @Autowired
    private OffLineDataExtendLoader extendLoader;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        //解决代理类时获取不到主注解的问题
        Offline offline = AnnotationUtils.findAnnotation(bean.getClass(),Offline.class);

        if(offline != null && StringUtils.isNotEmpty(offline.dataFrom())){
            extendLoader.addExtensionService(offline.dataFrom(), bean);
        }

        return bean;
    }
}
